<template>
    <el-dialog :visible.sync="showAddModal" :append-to-body="true"
               :close-on-click-modal="false" width="40%"
               :close-on-press-escape="false" :show-close="false" class="ele-modal">
        <h4 slot="title">{{modalLabel}}</h4>
        <el-form :model="newForm" class="demo-form-inline" :rules="rules" ref="ruleForm">
            <el-row>
                <el-col :span="16">
                    <el-form-item label="用户名" :label-width="formLabelWidth" prop="username">
                        <el-input v-model="newForm.username" auto-complete="off"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="16">
                    <el-form-item label="密码" :label-width="formLabelWidth" prop="password">
                        <el-input type="password" v-model="newForm.password" auto-complete="off"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="16">
                    <el-form-item label="邮箱" :label-width="formLabelWidth" prop="email">
                        <el-input v-model="newForm.email" auto-complete="off"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="16">
                    <el-form-item label="手机号" :label-width="formLabelWidth" prop="mobile">
                        <el-input v-model="newForm.mobile" auto-complete="off"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="16">
                    <el-form-item label="角色" :label-width="formLabelWidth" class="ele-checkbox">
                        <el-checkbox-group v-model="newForm.roleIdList">
                            <el-checkbox label="1">测试</el-checkbox>
                        </el-checkbox-group>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="16">
                    <el-form-item label="状态" :label-width="formLabelWidth">
                        <el-radio-group v-model="newForm.status">
                            <el-radio :label="0">禁用</el-radio>
                            <el-radio :label="1">正常</el-radio>
                        </el-radio-group>
                    </el-form-item>
                </el-col>
            </el-row>


        </el-form>
        <div slot="footer" class="row" style="text-align: center">
            <a class="revision-btn" href="javaScript:void(0)" @click="onSubmit">保 存</a>
            <a class="revision-btn-default" style="margin-left: 5px;" href="javaScript:void(0)" @click="onCancel">取
                消</a>
        </div>
    </el-dialog>
</template>

<script>

    export default {
        name: "addManagerModal",
        props: {
            showAddModal: {
                type: Boolean,
                default: false
            },
            editId: {
                type: String,
                default: ''
            }
        },
        data() {
            const validMobile = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('手机号不能为空！'));
                } else {
                    if (value !== '') {
                        const reg = /^1[3|4|5|7|8][0-9]\d{8}$/
                        if (!reg.test(value)) {
                            callback(new Error('请输入正确的手机号！'));
                        }
                    }
                    callback();
                }
            };

            return {
                modalLabel: '新增分类',
                newForm: {
                    username: '',
                    password: '',
                    email: '',
                    mobile: '',
                    roleIdList: [],
                    status: 1
                },
                formLabelWidth: '100px',
                dialogImageUrl: '',
                dialogVisible: false,
                fileList: [],
                uploadFiles: [],
                uploadFile: {},
                uploadUrl: '/sys/oss/upload',
                formData: {},
                rules: {
                    username: [
                        {required: true, message: '请输入用户名', trigger: 'blur'}
                    ],
                    password: [
                        {required: true, message: '请输入密码', trigger: 'blur'}
                    ],
                    email: [
                        {required: true, message: '请输入邮箱地址', trigger: 'blur'},
                        {type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur,change'}
                    ],
                    mobile: [
                        {required: true, validator: validMobile, trigger: "blur"}
                    ]
                }
            }
        },
        mounted: function () {
        },
        methods: {
            onCancel() {
                this.$refs.ruleForm.resetFields();
                this.$emit('onCancelNewModal');
            },
            onSubmit() {
                this.$refs.ruleForm.validate((valid) => {
                    if (valid) {
                        if (this.$isEmptyStr(this.editId)) {
                            this.submitNewInfo();
                        } else {
                            this.submitEditInfo();
                        }
                    } else {
                        return false;
                    }
                });

            },
            handleRemove(file, fileList) {
                this.fileList = fileList;
            },
            handleExceed(file, fileList) {
                this.$message.warn('文件重复');
            },
            handleChange(file, fileList) {
                this.uploadFile = file.raw;
                this.fileList = fileList;
            },
            handlePictureCardPreview(file) {
                this.dialogImageUrl = file.url;
                this.dialogVisible = true;
            },
            uploadRequest(file) {
                // this.subPicForm(param.raw);
                this.uploadFile = file.file;
                this.formData = new FormData();
                this.formData.append('file', this.uploadFile);
            },
            subPicForm() {
                this.$refs.upload.submit();
                this.submitUpload();
                let config = {
                    headers: {
                        'content-type': 'multipart/form-data'
                    }
                }

            },
            async submitUpload(file) {
                let res = await this.$api.post(this.uploadUrl, this.formData);
                if (res.code === 0) {
                } else {
                    this.$message.error(res.msg);
                }
            },
            async getInfo(editId) {
                let res = await this.$api.get('/sys/user/info/' + editId, {});
                if (res.code === 0) {
                    this.newForm.username = res.user.username;
                    this.newForm.email = res.user.email;
                    this.newForm.mobile = res.user.mobile;
                    this.newForm.status = res.user.status;
                    this.newForm.roleIdList = res.user.roleIdList;
                } else {
                    this.$message.error(res.msg);
                }
            },
            async submitEditInfo() {
                let res = await this.$api.post('/sys/user/update', this.newForm);
                if (res.code === 0) {
                    this.$message.success('保存成功');
                    this.$refs.ruleForm.resetFields();
                    this.$emit('onSubmitNewModal');
                } else {
                    this.$message.error(res.msg);
                }
            },
            async submitNewInfo() {
                let res = await this.$api.post('/sys/user/save', this.newForm);
                if (res.code === 0) {
                    this.$message.success('保存成功');
                    this.$refs.ruleForm.resetFields();
                    this.$emit('onSubmitNewModal');
                } else {
                    this.$message.error(res.msg);
                }
            },
        }
    }
</script>

<style scoped>
    .ele-modal /deep/ .el-form-item__label {
        font-family: "open sans", "Helvetica Neue", Helvetica, Arial, sans-serif;
        font-size: 12px;
        color: #676a6c;
    }

    .ele-modal /deep/ .el-radio__label {
        font-family: "open sans", "Helvetica Neue", Helvetica, Arial, sans-serif;
        font-size: 12px;
        color: #676a6c;
        font-weight: normal;
    }

    .ele-modal /deep/ .el-select {
        display: inline;
    }

    /deep/ .el-upload__input {
        display: none;
    }

    /deep/ .el-dialog__wrapper {
        z-index: 3000;
    }

    .ele-modal /deep/ .el-dialog__header {
        border-bottom: 1px solid #e5e5e5;
        padding: 5px 0 2px 15px !important;
    }

    .ele-modal /deep/ .el-dialog__body {
        padding: 20px;
    }

    .ele-modal /deep/ .el-dialog__footer {
        border-top: 1px solid #e5e5e5;
        padding: 15px;
    }
    .ele-checkbox /deep/ .el-checkbox__label{
        font-size: 12px;
    }
</style>
